package com.parking.carsystem.parkingchargesystem.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.module.GroupListModule;

import java.util.ArrayList;

import butterknife.BindView;
import ttyy.com.recyclerexts.base.EXTRecyclerAdapter;
import ttyy.com.recyclerexts.base.EXTViewHolder;

public class UpdateOrDeleteGroupActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.setting)
    TextView setting;
    @BindView(R.id.group_list)
    RecyclerView groupList;
    @BindView(R.id.layout_delete_cancle)
    LinearLayout layoutDeleteCancle;
    @BindView(R.id.cancle)
    TextView cancle;
    @BindView(R.id.delete)
    TextView delete;
    private ArrayList<GroupListModule.DataBean> list;

    private boolean editeStatus;
    private boolean updateDeleteStatus;
    private EXTRecyclerAdapter<GroupListModule.DataBean> recyclerAdapter;
    private int position;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_update_delete_group;
    }

    @Override
    public void initView() {
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        setting.setOnClickListener(this);
        delete.setOnClickListener(this);
        cancle.setOnClickListener(this);
    }

    @Override
    public void initData() {
        final String title = getIntent().getStringExtra(Constant.TITLE);
        list = getIntent().getParcelableArrayListExtra(Constant.GROUP_LIST);
        text.setText(title);
        if (title.equals("删除分组")) {
            setting.setVisibility(View.VISIBLE);
            updateDeleteStatus = true;
        }
        groupList.setLayoutManager(new LinearLayoutManager(this));
        recyclerAdapter = new EXTRecyclerAdapter<GroupListModule.DataBean>(R.layout.item_plate_group_edite) {
            @Override
            public void onBindViewHolder(EXTViewHolder extViewHolder, int i, GroupListModule.DataBean module) {
                ImageView choose_but = extViewHolder.findViewById(R.id.choose_but);
                extViewHolder.setText(R.id.group_name, module.groupName);
                if (editeStatus) {
                    choose_but.setVisibility(View.VISIBLE);
                } else {
                    choose_but.setVisibility(View.GONE);
                }
                if (module.chooseStatus) {
                    choose_but.setImageResource(R.mipmap.bt_xuanze_press);
                } else {
                    choose_but.setImageResource(R.mipmap.bt_xuanze);
                }
            }
        };
        recyclerAdapter.setDatas(list);
        groupList.setAdapter(recyclerAdapter);
        recyclerAdapter.setOnItemClickListener(new EXTRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClicked(View view, int i) {
                GroupListModule.DataBean parkingPlateGroupModule = list.get(i);
                if (updateDeleteStatus) {
                    //表示选中这个分组
                    if (parkingPlateGroupModule.chooseStatus) {
                        parkingPlateGroupModule.chooseStatus = false;
                    } else {
                        parkingPlateGroupModule.chooseStatus = true;
                    }
                    recyclerAdapter.notifyDataSetChanged();
                } else {
                    //跳转到下一界面修改分组名字
                    showToast("修改分组名字");
                    Intent intent = new Intent(UpdateOrDeleteGroupActivity.this, UpdateGroupActivity.class);
                    intent.putExtra(Constant.GROUP_NAME, parkingPlateGroupModule);
                    intent.putExtra(Constant.GROUP_POSITION, i);
                    startActivityForResult(intent, 0);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                if (position != -1) {
                    exitFinish();
                }
                break;
            case R.id.setting:
                if (layoutDeleteCancle.getVisibility() == View.GONE) {
                    layoutDeleteCancle.setVisibility(View.VISIBLE);
                }
                editeStatus = true;
                recyclerAdapter.notifyDataSetChanged();
                break;
            case R.id.cancle:
                layoutDeleteCancle.setVisibility(View.GONE);
                for (GroupListModule.DataBean parkingPlateGroupModule : list) {
                    if (parkingPlateGroupModule.chooseStatus) {
                        parkingPlateGroupModule.chooseStatus = false;
                    }
                }
                editeStatus = false;
                recyclerAdapter.notifyDataSetChanged();
                break;
            case R.id.delete:
                layoutDeleteCancle.setVisibility(View.GONE);
                StringBuilder builder = new StringBuilder();
                for (int i = list.size() - 1; i >= 0; i--) {
                    GroupListModule.DataBean dataBean = list.get(i);
                    if (dataBean.chooseStatus) {
                        list.remove(dataBean);
                        builder.append(dataBean.id);
                        if (i != 0) {
                            builder.append(",");
                        }
                    }
                }
                editeStatus = false;
                recyclerAdapter.notifyDataSetChanged();
                builder.deleteCharAt(builder.toString().length() - 1);
                deleteGroup(builder.toString());
                break;
        }
    }

    /**
     * 删除分组
     *
     * @param ids 分组id 6170941768e7403f81e909c0b5a2a578,138a4ade72424ee9afe78322257ee8ec,e7a4428634114699b1e910f601ee924e
     */
    private void deleteGroup(String ids) {
        OkGo.<String>delete(Constant.url + Constant.DELETEGROUPSREMOVE + ids)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseJson(response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        showToast(response.message());
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Constant.UPDATEGROUPNAME) {
            position = data.getIntExtra(Constant.GROUP_POSITION, -1);
            String groupName = data.getStringExtra(Constant.GROUP_NAME);
            if (position != -1) {
                GroupListModule.DataBean dataBean = list.get(position);
                dataBean.groupName = groupName;
                recyclerAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitFinish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exitFinish() {
        setResult(Constant.ADDPARKINGGROUPRESULTCODE);
        finish();
    }
}
